package com.module.compose.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

/**
 * Create： zhangjunfei 1/18/21 2:37 PM
 * 描述：
 */
abstract class BaseFragment<T : ViewDataBinding> : Fragment() {
    lateinit var binding: T

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, bindLayout(), container, false)
        binding.lifecycleOwner = this
        initView()
        initData()
        initListener()
        return binding.root
    }

    protected open fun initView() {}
    protected open fun initData() {}
    protected open fun initListener() {}
    protected abstract fun bindLayout(): Int

    fun hideSoftKeyboard() {
        val imm =
            requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    /**
     * 界面跳转
     */
    protected fun goTo(bundle: Bundle? = null, clazz: Class<*>) {
        val intent = Intent()
        context?.let { intent.setClass(it, clazz) }
        bundle?.let { intent.putExtras(it) }
        startActivity(intent)
    }

}